package sicau.carManager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import sicau.carManager.Util.SendMessage.SendMsgUtil;
import sicau.carManager.entity.Admin;
import sicau.carManager.entity.Message;
import sicau.carManager.result.R;
import sicau.carManager.service.AdminService;
import sicau.carManager.service.FixedCarInformationService;
import sicau.carManager.service.MessageService;
import sicau.carManager.service.ViolationInformationService;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ITcz
 * @Data 2021-02-06 - 15:10
 */
@Slf4j
@RestController
@RequestMapping("/supAdmin")
@CrossOrigin
public class SupAdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    ViolationInformationService vioService;

    @Autowired
    FixedCarInformationService fixService;

    @Autowired
    MessageService messageService;

    @Value("${spring.profiles.active}")
    private String env;

    @ApiOperation("显示所有普通管理员的工号【同时也是登录账号】和姓名")
    @GetMapping("/show/all/Admin")
    public R showAllAdmin(Admin admin,
                          @ApiParam("页数【页码】") @RequestParam("pageNumber") int pageNumber,
                          @ApiParam("每页显示的信息数") @RequestParam("pageSize") int pageSize){
        if (pageNumber < 0 || pageSize < 1) {
            return R.error(500601, "参数非法！");
        }
        String campus = admin.getCampus();
        IPage<Admin> adminIPage = adminService.showAllAdmin(pageNumber, pageSize, campus);
        HashMap resMap = new HashMap();
        resMap.put("total", adminIPage.getTotal());
        resMap.put("result", adminIPage.getRecords());
        return R.ok(resMap);
    }

    @ApiOperation("添加管理员账号")
    @PostMapping("/add/admin")
    public R addAdmin(Admin admin, @ApiParam("需要传的参数：authority，jobNumber【以此为登录账号】，name，password") @RequestBody Map<String,Object> map){
        if (map == null) {
            return R.error(500300, "添加管理员信息为空！");
        }
        Admin admin1 = new Admin();
        admin1.setAuthority((Integer) map.get("authority"));
        admin1.setCampus(admin.getCampus());
        admin1.setJobNumber((String) map.get("jobNumber"));
        admin1.setName((String) map.get("name"));
        admin1.setPassword((String) map.get("password"));
        int i = adminService.addOne(admin1);
        if (i == 0) {
            return R.error(500301, "数据库插入信息错误！");
        }
        return R.ok("添加成功!");
    }

    @ApiOperation("删除普通管理员账号")
    @PostMapping("/delete/admin")
    public R deleteAdmin(Admin admin, @ApiParam("被删除的管理员id") @RequestParam("id")String id){
        HashMap map = new HashMap();
        map.put("id", id);
        boolean b = adminService.removeByMap(map);
        if (!b) {
            return R.error(500303, "删除账号失败!");
        }
        return R.ok("删除成功！");
    }

    @ApiOperation("根据车牌号，生成违章警示短信内容返回内容和手机号(可能为空)")
    @GetMapping("/create/alert/sms")
    public R sendAlertSms(@ApiParam("车牌号") @RequestParam("licensePlate") String licensePlate){

        Integer count = messageService.selectCountByLicensePlate(licensePlate);
        String message = SendMsgUtil.createMsg(count, licensePlate);

        String phoneNumber = fixService.getPhoneNumberByLicensePlate(licensePlate);

        HashMap<String, Object> map = new HashMap();
        map.put("message", message);
        map.put("phoneNumber", phoneNumber);

        return R.ok(map);
    }

    @ApiOperation("对违章多次的车辆发送警示短信")
    @GetMapping("/send/alert/sms")
    public R sendAlertSms(@ApiParam("车牌号") @RequestParam("licensePlate") String licensePlate,
                          @ApiParam("手机号码") @RequestParam("phoneNumber") String phoneNumber,
                          @ApiParam("警示信息") @RequestParam("message") String message){
        if ("prod".equals(env)) {
            String response = SendMsgUtil.sendMsg(message, phoneNumber);
            //根据返回值判断是否请求成功
            if (! (response.contains("200") && response.contains("OK"))) {
                return R.error("发送失败message：" + response);
            }
        }
        log.info("当前环境为：" + env);

        //保存发送记录
        Message message1 = new Message();
        message1.setContent(message);
        message1.setPhoneNumber(phoneNumber);
        message1.setLicensePlateNumber(licensePlate);
        messageService.save(message1);

        return R.ok();
    }

}




